# 作业：
# 1、整理类的笔记到小红书

# 2、定义一个学生Student类。有下面的类属性：
# 1 姓名 name
# 2 年龄 age
# 3 成绩 score（语文，数学，英语) [每课成绩的类型为整数]
# 类方法：
# 1 获取学生的姓名：get_name() 返回类型:str
# 2 获取学生的年龄：get_age() 返回类型:int
# 3 返回3门科目中最高的分数。get_course() 返回类型:int
# 4 将每个实例学生对象写进txt文件里，要持久保存
# 举例：{"姓名":"a","年龄":29,"成绩":{"语文":99,"数学":100,"英语":85}
# 5 获取所有学生的信息
# 写好类以后，可以定义2个同学测试下:
# zm = Student('zhangming',20,[69,88,100])
# 返回结果：
# zhangming
# 20


# class student():
#     def __init__(self,name,age,score):
#         self.name=name
#         self.age=age
#         self.score=score
#     def score_(self):
#         f=max(self.score)
#         return f"3门科目中最高的分数是{f}"
#     def i(self):
#         dict1= {'语文': self.score[0], '数学': self.score[1], '英语': self.score[2]}
#         return "大家好，我的名字是{},今年{}岁，成绩是{}".format(self.name,self.age,dict1)
#     def set(self):
#         import os
#         dict1={}
#         dict1['姓名'] = self.name
#         dict1['年龄'] = self.age
#         dict1['成绩'] = {'语文': self.score[0], '数学': self.score[1], '英语': self.score[2]}
#         path = './student4'
#         with open(path, "a", encoding='utf-8') as f:
#             f.write(str(dict1))
#             f.write('\n')
#             return ""
#     def look(self):
#             with open(path, "r", encoding='utf-8') as f:
#                 return f.read()
# s1=student("jjj",23,[99,100,85])
# s2=student("yyy",22,[110,150,98])
# print(s1.i())
# print(s1.score_())
# print(s2.i())
# print(s2.score_())
# print(s1.set())
# print(s2.set())
#
#





# 3、定义一个字典类：dictclass。完成下面的功能：
# dict = dictclass({你需要操作的字典对象})
# 1 删除某个key
# del_dict(key)
# 2 判断某个键是否在字典里，如果在返回键对应的值，不存在则返回"not found"
# get_dict(key)
# 3 返回键组成的列表：返回类型;(list)
# get_key()
# 4 合并字典，并且返回合并后字典的values组成的列表。返回类型:(list)
# update_dict({要合并的字典})
# 100

# class dictclass():
#     def __init__(self,dict):
#         self.dict=dict
#     def del_dict(self,key):
#         if key in self.dict:
#             del self.dict[key]
#         else:
#             return "key不存在"
#     def get_dict(self,key):
#         if key in self.dict:
#             return self.dict[key]
#         else:
#             return "not found"
#     def get_key(self):
#         return list(self.dict.keys())
#     def update_dict(self,dict2):
#         self.dict.update(dict2)
#         return list(self.dict.values())
#
# dict1 = {"小天才":"19","姜":"20","kk":"123","下头":"666"}
# dict_obj = dictclass(dict1)
# dict_obj.del_dict("小天才")
# print(dict_obj.dict)
# dict_obj.del_dict("小大大")
# print(dict_obj.dict)
# value = dict_obj.get_dict('姜')
# print(value)
# value = dict_obj.get_dict('五千多')
# print(value)
# keys = dict_obj.get_key()
# print(keys)
# dict2 = {"shenme":"asdfgh"}
# new_dict = dict_obj.update_dict(dict2)
# print(dict_obj.dict)
# print(new_dict)







# 4、定义一个列表的操作类：Listinfo
# 包括的方法:
# 1 列表元素添加: add_key(keyname) [keyname:字符串或者整数类型]
# 2 列表元素取值：get_key(num) [num:整数类型]
# 3 列表合并：update_list(list) [list:列表类型]
# 4 删除并且返回最后一个元素：del_key()

# class Listinfo():
#     def __init__(self,list1):
#         self.list1 = list1
#     def add_key(self,keyname):
#         self.keyname =keyname
#         list1.append(keyname)
#     def get_key(self,num):
#         if 0 <= num < len(self.list1):
#             return self.lst[num]
#         else:
#             return None
#     def update_list(self, list1):
#         if isinstance(list1, list):
#             self.list1.extend(list1)
#     def del_key(self):
#         if len(self.list1) > 0:
#             return self.lst.pop()
#         else:
#             return None







# 5、做一个学生点名系统，系统里支持：存入学生、更改学生、删除学生、点到学生姓名方法
# 存入学生（姓名、年龄、家庭地址、手机号）： 存入成功后，提示xxx存入成功
# 更改学生（姓名、年龄、家庭地址、手机号）：可以修改学生内容的任意一个字段
# 删除学生：删除的学生将不在展示
# 展示全部学生：可以看到全部的学生姓名
# 学生姓名：随机抽取任意一个存在的学生信息，然后打印：到！。举例： 刘闯到！


# class Student:
#     def __init__(self, name, age, address, phone):
#         self.name = name
#         self.age = age
#         self.address = address
#         self.phone = phone
# class StudentAttendanceSystem:
#     def __init__(self):
#         self.students = []
#     def add_student(self, name, age, address, phone):
#         student = Student(name, age, address, phone)
#         self.students.append(student)
#         print(f"{name}存入成功")
#     def update_student(self, name, age=None, address=None, phone=None):
#         for student in self.students:
#             if student.name == name:
#                 if age is not None:
#                     student.age = age
#                 if address is not None:
#                     student.address = address
#                 if phone is not None:
#                     student.phone = phone
#                 print(f"{name}信息已更新")
#                 break
#             else:
#                 print(f"找不到姓名为{name}的学生")
#     def delete_student(self, name):
#         for student in self.students:
#             if student.name == name:
#                 self.students.remove(student)
#                 print(f"{name}已删除")
#                 return
#         print(f"找不到姓名为{name}的学生")
#     def display_students(self):
#         names = [student.name for student in self.students]
#         print("全部学生姓名：")
#         print(names)
#     def mark_attendance(self, name):
#         for student in self.students:
#             if student.name == name:
#                 print(f"{name}到！")
#                 return
#         print(f"找不到姓名为{name}的学生")
# attendance_system = StudentAttendanceSystem()
# attendance_system.add_student("刘闯", 18, "北京市", "123456789")
# attendance_system.add_student("小天才", 19, "上海市", "987654321")
# attendance_system.update_student("刘闯", age=19, phone="111111111")
# attendance_system.delete_student("小天才")
# attendance_system.display_students()
# attendance_system.mark_attendance("刘闯")





# 6、点餐管理系统
# 1》、餐馆可以新增菜品、下架菜品、修改菜品
# 2》、每个菜品，都有对应的库存，所有菜品的库存均为999，每出一道菜，库存-1
# 菜品有对应的价格，餐厅老板定义价格每道菜不允许超过15元，没有小数单位，均为整数
# 3》、餐馆后台有点赞系统，能够收集客户对菜品的喜好，0~5分
# 4》、有菜品评分提报功能，对平均分数（菜品总分/打分人数）低于3分的菜品进行强制下架；

# class Dish:
#     def __init__(self, name, price, stock):
#         self.name = name
#         self.price = price
#         self.stock = stock
#         self.likes = []
#     def update_price(self, price):
#         if price <= 15:
#             self.price = price
#             print(f"{self.name}的价格已更新为{price}元")
#         else:
#             print("菜品价格不能超过15元")
#     def add_like(self, rating):
#         if rating >= 0 and rating <= 5:
#             self.likes.append(rating)
#             print(f"已收集到一次{self.name}的评分：{rating}分")
#         else:
#             print("评分范围应在0到5分之间")
#     def get_average_rating(self):
#         if len(self.likes) > 0:
#             total_rating = sum(self.likes)
#             average_rating = total_rating / len(self.likes)
#             return average_rating
#         else:
#             return 0
#     def decrease_stock(self):
#         if self.stock > 0:
#             self.stock -= 1
#             print(f"{self.name}库存减少1份，当前剩余库存：{self.stock}")
#         else:
#             print(f"{self.name}已售罄")
# class Restaurant:
#     def __init__(self):
#         self.menu = {}
#     def add_dish(self, name, price):
#         if name not in self.menu:
#             dish = Dish(name, price, 999)
#             self.menu[name] = dish
#             print(f"{name}菜品已添加到菜单")
#         else:
#             print(f"{name}菜品已存在于菜单中")
#     def remove_dish(self, name):
#         if name in self.menu:
#             del self.menu[name]
#             print(f"{name}菜品已下架")
#         else:
#             print(f"{name}菜品不存在于菜单中")
#     def update_dish_price(self, name, price):
#         if name in self.menu:
#             dish = self.menu[name]
#             dish.update_price(price)
#         else:
#             print(f"{name}菜品不存在于菜单中")
#     def get_dish_stock(self, name):
#         if name in self.menu:
#             dish = self.menu[name]
#             return dish.stock
#         else:
#             return None
#     def add_dish_like(self, name, rating):
#         if name in self.menu:
#             dish = self.menu[name]
#             dish.add_like(rating)
#         else:
#             print(f"{name}菜品不存在于菜单中")
# restaurant = Restaurant()
# restaurant.add_dish("小天才", 12)
# restaurant.add_dish("坤架", 15)
# restaurant.add_dish("大坤架", 14)
# restaurant.remove_dish("大坤架")
# restaurant.update_dish_price("小天才", 13)
# print(restaurant.get_dish_stock("小天才"))
# restaurant.add_dish_like("小天才", 4)
# restaurant.add_dish_like("小天才", 5)







# 7、定义一个桌子类（Desk），包含长（length）、宽（width）、高（height）属性，
# 包含一个打印桌子信息属性的方法（showInfo）。实例化2个桌子对象，为其赋予不同的属性值，
# 并调用showInfo方法，输出每个桌子的信息。

# class Desk():
#     def __init__(self,length,width,height):
#         self.length=length
#         self.width=width
#         self.height=height
#     def showInfo(self):
#         print("长:{},宽:{},高:{}".format(self.length,self.width,self.height))
# Desk1 = Desk(10,15,20)
# Desk2 = Desk(70,80,100)
# Desk1.showInfo()
# Desk2.showInfo()



# 8、定义一个用户类(User),用户名(username)和密码(password)是这个类的属性。


# class user():
#     def __init__(self,username,password):
#         self.username=username
#         print("账号为{}".format(username))
#         self.password=password
#         print("密码为{}".format(password))
# user("admin","123456")


# 9、定义一个计算器类，有加减乘除四个方法

# class calculator():
#     def add(self,a,b):
#         return a+b
#     def subtraction(self,a,b):
#         return a-b
#     def multiplication(self,a,b):
#         return a*b
#     def division(self,a,b):
#         if a != 0:
#             return a/b
#         else:
#             return "参数错误！"
# print(calculator().add(5,3))
# print(calculator().subtraction(10,1))
# print(calculator().multiplication(8,7))
# print(calculator().division(0,10))
# print(calculator().division(30,10))





# 10、创建一个名为Person的类，具有以下属性和方法：
# 属性：name（姓名），age（年龄）
# 方法：greet()，打印出一句问候语，包括该人的姓名和年龄

# class Person():
#     def __init__(self,name,age):
#         self.name = name
#         self.age = age
#     def greet(self):
#         print("我叫{},我今年{}".format(self.name,self.age))
# person1 = Person("牛爷爷",50)
# person1.greet()




# 11、定义一个图书管理系统类，图书可以新增、修改、删除，要求新增的每个图书都加到txt文本里，数据持久化
# 新增：图书名、作者名称、新建年月日
# 修改：图书名、作者名称、新建年月日
# 删除：图书删掉，且不在展示
# 查看：全量查询，精确查询，需要用户传入形参来控制返回结果

# class BookManagerSystem:
#     def __init__(self):
#         self.book_dict = {}
#     def add_book(self,name,Author,time1):
#         with open('./book.txt','a',encoding='utf-8') as f:
#             f.write(f"图书名:{name}")
#             f.write(f"作者名称:{Author}")
#             f.write(f"新建年月日:{time1}" + "\n")
#         self.book_dict.update({name:{"作者名称":Author,"新建年月日":time1}})
#     def update_book(self,old_name,new_name,Author,time1):
#         if old_name in self.book_dict.keys():
#             self.book_dict.update({new_name:{"作者名称":Author,"新建年月日":time1}})
#             self.book_dict.pop(old_name)
#             return 1
#         else:
#             return "图书不存在，无法修改！"
#     def delete_book(self,name):
#         try:
#             for i in self.book_dict.keys():
#                 if i in name:
#                     self.book_dict.pop(i)
#         except:
#             return f"图书已经被删掉"
#     def search_book(self,type,name = None): # type = 1 ：精确查询 ，type = 2 ：全量查询
#         num = 0
#         if type == 1:
#             if name:
#                 for i in self.book_dict.keys():
#                     if i in name: # keys()
#                         return f"您要查的图书名为：{name} , 作者名称为：{self.book_dict[i]['作者名称']} ,新建年月日为： {self.book_dict[i]['新建年月日']}"
#         elif type == 2:
#             for key,value in self.book_dict.items():
#                 num +=1
#                 print(f"第{num}本书的名称为：{key},作者名称为：{self.book_dict[key]['作者名称']}，新建年月日为：{self.book_dict[key]['新建年月日']}")









# 12、创建一个名为"Car"的类，该类具有"品牌"和"颜色"属性。
# 创建一个"start_engine"方法，该方法打印出"启动汽车引擎！"的消息。
# 创建一个"stop_engine"方法，该方法打印出"关闭汽车引擎！"的消息

# class Car():
#     def __init__(self,brand,colour):
#         self.brand= brand
#         self.colour=colour
#         print("品牌：{},颜色：{}".format(self.brand, self.colour))
#     def start_engine(self):
#         print("启动汽车引擎！")
#     def stop_engine(self):
#         print("关闭汽车引擎！")
# Car("奔驰一号","绿色")
# Car("红旗三号","黑色").start_engine()
# Car("三轮","24K金色").stop_engine()






# 13、淘宝商家后台，可以上架商品、下架商品，功能如下：
#  （1） 可以添加商品，商家添加完的商品会存到商品列表
#  1.1  商品要有名称、sku、价格
#  （2） 可以修改商品信息：名称、sku、价格
#  （3） 可以对商品进行下架，下架后的商品不再展示


# class Product:
#     def __init__(self, name, sku, price):
#         self.name = name
#         self.sku = sku
#         self.price = price
#         self.is_active = True
#     def update_info(self, name, sku, price):
#         self.name = name
#         self.sku = sku
#         self.price = price
#     def deactivate(self):
#         self.is_active = False
# class Store:
#     def __init__(self):
#         self.products = []
#     def add_product(self, name, sku, price):
#         product = Product(name, sku, price)
#         self.products.append(product)
#     def find_product_by_sku(self, sku):
#         for product in self.products:
#             if product.sku == sku:
#                 return product
#         return None
#     def update_product_info(self, sku, name=None, new_sku=None, price=None):
#         product = self.find_product_by_sku(sku)
#         if product:
#             if name:
#                 product.name = name
#             if new_sku:
#                 product.sku = new_sku
#             if price:
#                 product.price = price
#             print("商品信息已更新")
#         else:
#             print("找不到该商品")
#     def deactivate_product(self, sku):
#         product = self.find_product_by_sku(sku)
#         if product:
#             product.deactivate()
#             print("商品已下架")
#         else:
#             print("找不到该商品")
#             def print_product_inventory(self):
#         print("商品库存:")
#         for product in self.products:
#             status = "上架" if product.is_active else "下架"
#             print(f"名称：{product.name} SKU：{product.sku} 价格：{product.price} 状态：{status}")
# store = Store()
# store.add_product("商品1", "SKU1", 10.99)
# store.add_product("商品2", "SKU2", 19.99)
# store.add_product("商品3", "SKU3", 25.99)
# store.update_product_info("SKU2", name="新商品2", price=29.99)
# store.deactivate_product("SKU3")
# store.print_product_inventory()



# 14、定义一个人类
# 1.小明和小美都爱跑步
# 2.小美体重45.0公斤
# 3.小明体重75.0公斤
# 4.每次跑步都会减少0.5公斤
# 5.每次吃东西都会增加1公斤

# class person() :
#     def __init__(self,name,weight):
#         self.name=name
#         self.weight =weight
#     def run(self):
#         print("{}跑步去了".format(self.name))
#         self.weight -= 0.5
#     def eat(self):
#         print("{}又吃上了".format(self.name))
#         self.weight += 1
# ming = person("小明",75)
# mei = person("小美",45)
# ming.run()
# mei.eat()
# print("小明的体重为{}".format(ming.weight))
# print("小美的体重为{}".format(mei.weight))

# 15、士兵瑞恩有一把ak47
# 士兵可以开火（开火扣动的是扳机）
# 枪能够发射子弹（把子弹打出去）
# 抢能够装子弹–增加子弹数量
# 要求没子弹，需要输出没有子弹了，且发射失败



class Gun:
    def __init__(self, model, bullets):
        self.model = model
        self.bullets = bullets
        self.max_bullets = 30
    def shoot(self):
        if self.bullets > 0:
            print("开火！")
            self.bullets -= 1
        else:
            print("没有子弹了，发射失败！")
    def add_bullets(self):
        if self.bullets < self.max_bullets:
            self.bullets += 1
            print("装填一颗子弹")
        else:
            print("备弹已经满了！")
class Soldier:
    def __init__(self, name, gun):
        self.name = name
        self.gun = gun
    def fire(self):
        self.gun.shoot()
    def add_bullets(self):
        self.gun.add_bullets()

gun1 = Gun("AK47", 30)
soldier1 = Soldier("瑞恩", gun1)
soldier1.fire()
soldier1.add_bullets()
soldier1.fire()















